<script setup>
import { ref } from 'vue'
import { getDeptByParent } from '@/api/index'
import TreeDept from "../../components/treeDept.vue";
const form = ref({})
const disableDept = ref(false)
const disDept = () => {
  disableDept.value = true
}
const userInfo = uni.getStorageSync('userInfo')
// const lazyDept = (node, resolve) => {
//   console.log('node', node)
//   let deptid = null
//   if(node.root) {
//     deptid = userInfo.deptId
//   } else {
//     deptid = node.value
//   }
//   const params = {
//     parentid: deptid
//   }
//   getDeptByParent(params).then(res => {
//     const result = res.data.map(item => {
//       return {
//         value: item.deptId,
//         text: item.deptName
//       }
//     })
//     resolve(result)
//   })
// }
// const changeDept = (...args) => {
//   console.log('changeDept', args, form.value)
// }
// const pathChangeDept = (...args) => {
//   console.log('pathChangeDept', args)
//   const depts = args[0]
//   const dept = depts[depts.length - 1] ? depts[depts.length - 1] : depts[depts.length - 2]
//   console.log('dept.length - 1', dept.length - 1)
//   form.value.deptId = dept.value
//   form.value.deptName = dept.text
// }
const obj2StrParams = (obj, firstStr = '?') => {
  let params = firstStr
  for (let p in obj) {
    params += p + "=" + obj[p] + "&"
  }
  return params.substring(0, params.length - 1)
}
// const handleGo = () => {
//   console.log('form', form.value)
//   const params = obj2StrParams(form.value)
//   console.log('params', params)
//   uni.navigateTo({
//     url: '/pages/tasks/index' + params
//   })
// }
const deptData = ref([
  {
    deptId: userInfo.deptId,
    deptName: userInfo.dept.deptName
  }
])
const changeDept = (dept) => {
  console.log('dept', dept)
  form.value.deptId = dept.deptId
  // form.value.deptName = dept.deptName
  const params = obj2StrParams(form.value)
  console.log('params', params)
  uni.navigateTo({
    url: '/pages/tasks/index' + params
  })
}
</script>

<template>
<!--  <nut-form :model-value="form" ref="formData">-->
<!--    <nut-form-item label="任务名称">-->
<!--      <nut-input v-model="form.name"  class="nut-input-text" placeholder="请输入姓名" type="text" />-->
<!--    </nut-form-item>-->
<!--    <nut-form-item label="部门">-->
<!--      <nut-input v-model="form.deptName"  class="nut-input-text" placeholder="请输入姓名" type="text" @focus="disDept" />-->
<!--      <nut-cascader-->
<!--          title="地址选择"-->
<!--          v-model:visible="disableDept"-->
<!--          v-model="form.deptId"-->
<!--          @change="changeDept"-->
<!--          @pathChange="pathChangeDept"-->
<!--          lazy-->
<!--          :lazyLoad="lazyDept"-->
<!--      ></nut-cascader>-->
<!--    </nut-form-item>-->
<!--    <view class="btn" style="padding: 40px 20px 0;">-->
<!--      <nut-button block type="primary" @click="handleGo">-->
<!--        提交-->
<!--      </nut-button>-->
<!--    </view>-->
<!--  </nut-form>-->
  <view class="title">请选择部门查看</view>
  <view class="box-tree">
    <tree-dept :data="deptData" @change="changeDept"></tree-dept>
  </view>
  <nut-safe-area></nut-safe-area>
</template>

<style scoped lang="scss">
.title{text-align: center;padding: 10px 0;font-size: 20px;}
.box-tree{padding: 0 20px 40px 0;}
</style>
